package com.raising.modules.sys.controller;

import com.google.common.collect.Lists;
import com.raising.framework.config.JConfig;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.dao.IDBSessionDAO;
import com.raising.modules.sys.entity.Sessions;
import com.raising.modules.sys.service.SessionService;
import com.raising.utils.FunctionsUtils;
import com.raising.utils.SerializableUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
* session管理 控制器
* @author LiHaoShan
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/sys/session")
public class SessionsController extends BaseController {

    @Autowired
    private SessionService sessionService;

//    @Autowired
//    private IDBSessionDAO sessionDAO;

    @Autowired
    private CachingSessionDAO sessionDAO;

    /**
     * 进入会话管理页面
     * @author GaoYuan
     * @datetime 2018/2/23 上午11:04
     */
    @RequiresPermissions("sys:session:view")
    @RequestMapping(value = "/list")
    public String list() {
        olog("进入会话管理页面");
        return "sys/session/list";
    }

    /**
     * 获取当前会话分页数据
     * @author GaoYuan
     * @datetime 2018/2/23 上午11:04
     */
    @RequiresPermissions("sys:session:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody
    ResultVo getPage(Model model, Page<Sessions> page) throws Exception{
        page.setOrderBy("a.create_date desc");
//        Collection<Session> sessionList = sessionDAO.getActiveSessions();
//        System.out.println(sessionList.size());
        page = sessionService.getPage(page);
        List<Session> list = Lists.newArrayList();
        for (int i=0;i<page.getResults().size();i++){
            Session session = SerializableUtils.deserialize(page.getResults().get(i).getSession());
            //获取session会话中的用户名
            String sessionAccount= FunctionsUtils.principal(session);
            session.setAttribute("sessionAccount",sessionAccount);
            list.add(session);
        }
        Page<Session> pages = new Page<>();
        pages.setResults(list);
        pages.setPageNo(page.getPageNo());
        pages.setPageSize(page.getPageSize());
        pages.setTotalRecord(page.getTotalRecord());
        model.addAttribute("page", pages);
        return ResultVo.success().desc("ok").data(pages);
    }

    /**
     * 强制退出指定会话
     * @author gaoyuan
     * @return
     */
    @RequiresPermissions("sys:session:forceLogout")
    @RequestMapping(value = "/forceLogout.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo deleteJson(String sessionId) {
        Session session = sessionDAO.readSession(sessionId);
        if (session != null) {
            session.setAttribute(JConfig.SESSION_FORCE_LOGOUT_KEY,Boolean.TRUE);
        }
        olog("强制退出指定会话","{sessionId:"+sessionId+"}");
        return ResultVo.success().desc("强制退出成功");
    }
}
